package com.its.controller;

import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ser.Serializers;
import com.its.domain.BaseResult;
import com.its.domain.SmsCode;
import com.its.utils.SmsSendUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/sms")
public class SmsController {
    //发送短信验证码
    @GetMapping("/sendSmsCode")
    public BaseResult  sendSmsCode(String memberPhone,String code){
        try {
            SendSmsResponse resp = SmsSendUtil.sendSms(memberPhone, new ObjectMapper().writeValueAsString(new SmsCode(code)));
            BaseResult result = new BaseResult(resp.getStatusCode(),resp.getBody().getMessage(),null);
            System.out.println("阿里云短信响应：");
            System.out.println("状态码：" + resp.getStatusCode());
            System.out.println("返回码：" + resp.getBody().getCode());
            System.out.println("信息：" + resp.getBody().getMessage());
            System.out.println("BizId：" + resp.getBody().getBizId());
            return result;
        } catch (Exception e) {
            System.out.println("阿里云短信发送失败");
            e.printStackTrace();
            BaseResult result = new BaseResult(400,"发送失败",null);
            return result;
        }
    }
}
